<?php

class team_details
{
	var $mode;
	var $data;
	
	var $displayJoin = false;
	
	var $drivers = '';
	
	var $items = Array();

	function team_details($data, $mode)
	{
		$this->data = $data;
		$this->mode = $mode;
		
		if(isset($_POST['joinTeam']) && isset($_GET['id']) && isset($_SESSION["valid_id"]))
		{
			$this->addMember();
		}
		
		if($mode == 'Mini')
		{
			$this->setupMini();
		}
		elseif($mode == 'information')
		{
			$this->setupInformation();
		}
		
	}
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
		
		if($this->mode == 'Mini')
		{
			$this->RenderMini();
		}
		else if($this->mode == 'information')
		{
			$this->renderInformation();
		}
	}
	
	function addMember() {
            global $www_path, $lang, $db;
            include_once('includes/addMessage.php');

            $qThisUser = 'SELECT pf_drivername as user_drivername FROM ' . DRIVERNAME_TABLE . ' where user_id = ' . $_SESSION["valid_id"];
            $qThisTeam = 'SELECT t_name, t_logo FROM ' . TEAMS_TABLE . ' where t_id = ' . $_GET['id'];
            $qAdmins = 'SELECT pf_drivername as user_drivername, user_email
					FROM ' . USERS_TABLE . ' u, ' . TEAMMEMBERS_TABLE . ', ' . DRIVERNAME_TABLE . ' d ' .
                'WHERE u.user_id = tm_driver AND u.user_id = d.user_id AND tm_admin = 1 AND tm_team = ' . $_GET['id'];
            $qUpdateDB = 'INSERT INTO ' . TEAMMEMBERS_TABLE . ' VALUES( ' .  $_SESSION["valid_id"] . ', ' .
                $_GET['id'] . ', 0, 0)';
            $res = $db->sql_query($qUpdateDB);
            $res = $db->sql_query($qThisUser);
            if( $submitUser = $db->sql_fetchrow($res)) {
                $res = $db->sql_query($qThisTeam);
                if( $thisTeam = $db->sql_fetchrow($res)) {

                    include_once('includes/email.php');
                    $thisTeam['userDrivername'] = $submitUser['user_drivername'];
                    $thisTeam['joinMessage'] = $_POST['joinMessage'];
                    $thisTeam['teamcp'] = $www_path . 'usercp.php?action=teamadmin';

                    $res = $db->sql_query($qAdmins);
                    while($thisAdmin = $db->sql_fetchrow($res)) {
                        $thisTeam['adminDrivername'] = $thisAdmin['user_drivername'];
                        $thisTeam['adminEmail'] = $thisAdmin['user_email'];

                        $email = new email('team_join_request', $thisTeam);

                        $email->addReplacement('{TEAM}' , 't_name', 1);
                        $email->addReplacement('{ADMIN}' , 'adminDrivername', 1);
                        $email->addReplacement('{EMAILADRESS}' , 'adminEmail', 1);
                        $email->addReplacement('{NEW_MEMBER}' , 'userDrivername', 1);
                        $email->addReplacement('{JOIN_MESSAGE}' , 'joinMessage', 1);
                        $email->addReplacement('{TEAM_CP}' , 'teamcp', 1);

                        if($thisTeam['t_logo'] != '') {
                            $email->addReplacement('{TEAM_LOGO}' , '<img src="' . $www_path . $thisTeam['t_logo'] . '"/>' );
                        }
                        else {
                            $email->addReplacement('{TEAM_LOGO}' , '' );
                        }

                        $email->sendEmail();

                    }

                    $this->items[] = new addMessage(str_replace('{team}',$thisTeam['t_name'],$lang['TeamJoinInfo']), $lang['TeamJoinTitle']);
                }
            }



        }

	
	function setupMini()
	{
		global $lang;
		if($this->data['t_drivers'] != '')
		{
			$this->drivers = '<B>' .  $lang['TeamDrivers'] . ':</B> ' . $this->data['t_drivers'];
		}
		else
		{
			$this->drivers = $lang['TeamNoDrivers'];
		}
		
		if($this->data['t_satteams'] != '')
		{
			$this->drivers .= '<br><B>' . $lang['TeamSatelite'] . ':</B> ' . $this->data['t_satteams'];
		}
	}
	
	function renderMini()
	{
		global $lang, $skin_dir;
		$drivers = StripSlashes($this->drivers);
		if($this->data['t_logo'] == '')
		{
			$logo = '';
		}
		else
		{
			$logo = '<a href="teams.php?id=' . $this->data['t_id'] . '"><img src="' . $this->data['t_logo'] . '" /></a>';
		}
		$title = '<a href="teams.php?id=' . $this->data['t_id'] . '">' . StripSlashes($this->data['t_name']) . '</a>';
		include $skin_dir . 'tpl/teamMini.tpl';
	}
	
	function renderItems()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
	}

	function renderInformation()
	{
		global $lang, $skin_dir;
		$drivers = $this->drivers;
		if($this->data['t_logo'] == '')
		{
			$logo = '';
		}
		else
		{
			$logo = '<img class="tableImageMid" src="' . $this->data['t_logo'] . '" alt="">';
		}
		$title = StripSlashes($this->data['t_name']);
		$parentTitle = $lang[$this->data['ParentType']];
		$parentText = $this->data['ParentText'];
		$description = formatOutput($this->data['t_description']);
		$nation = StripSlashes($this->data['t_nationality']);
		$website = $this->data['t_website'] == '' ? '' : '<a href="' . $this->data['t_website'] . '">' . $this->data['t_website'] . '</a>';
		$rem_website_start = $this->data['t_website'] ==  '' ? '<!--' : '';
		$rem_website_end = $this->data['t_website'] ==  '' ? '-->' : '';
		$rem_join_start = $this->displayJoin ? '' : '<!--' ;
		$rem_join_end = $this->displayJoin ? '' : '-->' ;
		$id = $this->data['t_id'];
		include $skin_dir . 'tpl/teamInformation.tpl';
	}
	
	
	function setupInformation()
	{
		global $db, $lang;
		$q = 'select pf_drivername as user_drivername, u.user_id, tm_admin 
			  from ' . USERS_TABLE . ' u, ' . TEAMMEMBERS_TABLE . ', '  . TEAMS_TABLE . ', ' . DRIVERNAME_TABLE . ' d ' .
				' WHERE u.user_id = tm_driver AND u.user_id = d.user_id AND tm_approved = 1 AND 
				        ((tm_team = ' . $this->data['t_id'] . ' AND t_id = tm_team) OR 
						(t_id = ' . $this->data['t_id'] . ' AND t_parent = tm_team))
				  GROUP BY u.user_id
				  ORDER BY pf_drivername ASC';
		$res = $db->sql_query($q);
		
		while ( $row = $db->sql_fetchrow($res))
		{
			$admin = $row['tm_admin'] == 1 ? ' (Admin)' : '';
			$this->drivers = $this->drivers .  '<a href="drivers.php?id=' .  $row['user_id'] . '"> ' . $row['user_drivername'] . $admin . '</a><br>';
		}
		$this->drivers = substr($this->drivers , 0, -4);
		
		if($this->data['t_parent'] > 0)
		{
			$this->data['ParentType'] = 'TeamParent';
			$q = 'SELECT t_id, t_name
				  FROM ' . TEAMS_TABLE .
				' WHERE t_id = ' . $this->data['t_parent'];
		}
		else
		{
			$this->data['ParentType'] = 'TeamSatelite';
			$q = 'SELECT t_id, t_name
				  FROM ' . TEAMS_TABLE .
				' WHERE t_parent = ' . $this->data['t_id'];
		}
		
		$res = $db->sql_query($q);
		$this->data['ParentText'] = '';
		
		while ( $row = $db->sql_fetchrow($res))
		{
			$this->data['ParentText'] .= '<a href="teams.php?id=' .  $row['t_id'] . '"> ' . $row['t_name'] . '</a><br>';
		}
		$this->drivers = substr($this->drivers , 0, -4);
		
		if (isset($_SESSION["valid_id"]) && $this->data['t_parent'] == 0)
		{
			$q = 'select tm_driver from ' . TEAMMEMBERS_TABLE .
				' WHERE tm_driver = ' . $_SESSION["valid_id"] . ' AND tm_team = ' . $this->data['t_id'];
			$res = $db->sql_query($q);
			if($row = $db->sql_fetchrow($res))
			{
				$this->displayJoin = false;
			}
			else
			{
				$this->displayJoin = true;
			}
		}
	}
}

?>